package cn.edu.scau.cmi.ema.dao;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.Session;


import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;


import cn.edu.scau.cmi.ema.util.CmiSqlUtil;

@Repository("FoodDAO")
@Transactional
public class FoodDAOImpl extends AbstractJpaDao<Food> implements FoodDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(Arrays.asList(new Class<?>[] { Food.class }));

	@PersistenceContext(unitName = "ema")
	private EntityManager entityManager;

	public FoodDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Food findFoodById(Integer id) throws DataAccessException {

		return findFoodById(id, -1, -1);
	}

	@Transactional
	public Food findFoodById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findFoodById", startResult, maxRows, id);
			return (Food) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Food> findFoodByName(String name) throws DataAccessException {

		return findFoodByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Food> findFoodByName(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findFoodByName", startResult, maxRows, name);
		return new LinkedHashSet<Food>(query.getResultList());
	}

	@Transactional
	public Set<Food> findFoodByNameContaining(String name) throws DataAccessException {

		return findFoodByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Food> findFoodByNameContaining(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findFoodByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Food>(query.getResultList());
	}

	@Transactional
	public Set<Food> findFoodByDescription(String description) throws DataAccessException {

		return findFoodByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Food> findFoodByDescription(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFoodByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Food>(query.getResultList());
	}

	@Transactional
	public Set<Food> findFoodByDescriptionContaining(String description) throws DataAccessException {

		return findFoodByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Food> findFoodByDescriptionContaining(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFoodByDescriptionContaining", startResult, maxRows, description);
		return new LinkedHashSet<Food>(query.getResultList());
	}

	@Transactional
	public Food findFoodByPrimaryKey(Integer id) throws DataAccessException {
		return findFoodByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Food findFoodByPrimaryKey(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findFoodByPrimaryKey", startResult, maxRows, id);
			return (Food) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Food> findFoodsByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findFoodsByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Food> findAllFoods() throws DataAccessException {

		return findAllFoods(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Food> findAllFoods(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllFoods", startResult, maxRows);
		return new LinkedHashSet<Food>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Food> findFoodsByBrand(Integer brandId, int start, int max) throws DataAccessException {
		Query query = createNamedQuery("findAllFoodsByBrand", start, max, brandId);
		return new LinkedHashSet<Food>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Food> findFoodsByExpire(Integer expireId, int start, int max) throws DataAccessException {
		Query query = createNamedQuery("findAllFoodsByExpire", start, max, expireId);
		return new LinkedHashSet<Food>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Food> findFoodsByStoremethod(Integer storemethodId, int start, int max) throws DataAccessException {
		Query query = createNamedQuery("findAllFoodsByStoremethod", start, max, storemethodId);
		return new LinkedHashSet<Food>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Food> findFoodsByProducer(Integer producerId, int start, int max) throws DataAccessException {
		Query query = createNamedQuery("findAllFoodsByProducer", start, max, producerId);
		return new LinkedHashSet<Food>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Food> findFoodsByFoodtype(Integer foodtypeId, int start, int max) throws DataAccessException {
		Query query = createNamedQuery("findAllFoodsByFoodtype", start, max, foodtypeId);
		return new LinkedHashSet<Food>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Food> findFoodsByProductcode(Integer productcodeId, int start, int max) throws DataAccessException {
		Query query = createNamedQuery("findAllFoodsByProductcode", start, max, productcodeId);
		return new LinkedHashSet<Food>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Food> findFoodsByTaste(Integer tasteId, int start, int max) throws DataAccessException {
		Query query = createNamedQuery("findAllFoodsByTaste", start, max, tasteId);
		return new LinkedHashSet<Food>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Food> findFoodByCompositeProperities(String sql, int startResult, int maxRows) {
		return new LinkedHashSet<Food>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Food> findFoodsByDetachedCriteria(CmiPagination page) throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Food.class);
		Session session = entityManager.unwrap(Session.class);
		Set<Food> foods = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return foods;
	}

	@Transactional
	public Food findFoodByPhysicalPrimaryKey(String name, Integer foodtypeId, Integer productcodeId, Integer brandId)
			throws DataAccessException {
		return findFoodByPhysicalPrimaryKey(name, foodtypeId, productcodeId, brandId, -1, -1);
	}

	@Transactional
	public Food findFoodByPhysicalPrimaryKey(String name, Integer foodtypeId, Integer productcodeId, Integer brandId,
			int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findFoodByPhyscialPrimaryKey", startResult, maxRows, name, foodtypeId,
					productcodeId, brandId);
			return (Food) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Food entity) {
		return true;
	}
}
